package com.easyUploader.entity.vo;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.util.MybatisUtil;
import com.common.util.StringUtil;
import com.common.validator.CustomNotEmpty;
import com.easyUploader.entity.dao.FileStorage;
import com.easyUploader.entity.dao.User;
import com.easyUploader.entity.dto.UserDto;
import com.easyUploader.mapper.FileStorageMapper;
import com.easyUploader.service.impl.UserServiceImpl;
import com.easyUploader.threadHolder.UserThreadHolder;
import com.easyUploader.util.FileUtil;
import com.security.event.UserDomainEventPublisher;
import com.security.service.AuthenticateService;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 大忽悠
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class UserUpdateVo {
    private String username;
    private String password;
    private MultipartFile headImgFile;

    public void updateUsernameOrPwdIfNeed(UserServiceImpl userService) {
        //判断是否需要更改用户名,如果需要,需要检查用户名是否重复
        //如果用户名与当前用户名一致,也无需更新
        if (!StringUtil.isNullOrEmpty(username)&&!UserThreadHolder.get().getUsername().equals(username)) {
            MybatisUtil.repeatCheck(userService, "username", username, "用户名重复");
            SpringUtil.getBean(UserDomainEventPublisher.class).publishUserUpdateEvent(username,UserThreadHolder.get().getUsername());
        } else {
            username = null;
        }
        //密码处理--如果密码没改,也无需更新
        if (StringUtil.isNullOrEmpty(password)||UserThreadHolder.get().getPassword().equals(password)) {
            password = null;
        }
        //不需要进行更新
        if(username==null&&password==null){
            return;
        }
        //更新
        userService.updateById(User.builder().username(username).password(getPassword()).id(UserThreadHolder.get().getId()).build());
    }

    public void updateHeadImgIfNeed(UserServiceImpl userService) throws IOException {
        if (headImgFile == null || headImgFile.isEmpty()) {
            return;
        }
        //当前用户信息
        UserDto userDto = UserThreadHolder.get();
        //删除旧的头像
        String oldHeadImgFilePath = delOldHeadImgRecord(userDto);
        //更新用户头像路径
        userService.updateById(User.builder().id(userDto.getId()).headImg(UserVo.getUploadedHeadImgFileRelativePath(headImgFile)).build());
        //上传新的头像
        UserVo.uploadHeadImgFile(headImgFile);
        //真正删除旧的头像数据
        delOldHeadImgFile(oldHeadImgFilePath);
    }

    private void delOldHeadImgFile(String oldHeadImgFilePath) {
        FileUtil.deleteFile(oldHeadImgFilePath);
    }

    private String delOldHeadImgRecord(UserDto userDto) {
        FileStorageMapper fileStorageMapper = SpringUtil.getBean(FileStorageMapper.class);
        FileStorage selectOne = fileStorageMapper.selectOne(new QueryWrapper<FileStorage>().eq("create_by", userDto.getId()).eq("file_visit_path", userDto.getHeadImg()));
        fileStorageMapper.deleteById(selectOne.getId());
        return selectOne.getFileSavePath();
    }
}
